﻿@page "/PivotGridLargeDataBase"

<DemoPageSectionComponent Id="PivotGrid-LargeDataSources">
    @inherits OwningComponentBase
    @inject Microsoft.Extensions.Configuration.IConfiguration Configuration

    <DataProviderAccessAreaContainer DataProvider="@ContosoRetailDataProvider">
        @if(DataSource == null) {
            <p><em>Loading...</em></p>
        } else {
            <DxPivotGrid Data="@DataSource"
                         ShowPager="true"
                         PageSize="10">
                <DxPivotGridField Field="@nameof(Sale.StoreName)" Caption="Store" Area="PivotGridFieldArea.Row" />
                <DxPivotGridField Field="@nameof(Sale.DateKey)" GroupInterval="PivotGridGroupInterval.Year" Caption="Year" Area="PivotGridFieldArea.Column" />
                <DxPivotGridField Field="@nameof(Sale.SalesAmount)" Caption="Amount" Area="PivotGridFieldArea.Data" SummaryType="PivotGridSummaryType.Sum">
                    <DataTemplate>@string.Format("{0:c0}", context)</DataTemplate>
                </DxPivotGridField>
                <DxPivotGridField Field="@nameof(Sale.Id)" Caption="Count" Area="PivotGridFieldArea.Data" SummaryType="PivotGridSummaryType.Count">
                    <DataTemplate>@string.Format("{0:n0}", context)</DataTemplate>
                </DxPivotGridField>
            </DxPivotGrid>
        }
    </DataProviderAccessAreaContainer>
    @code {
        IContosoRetailDataProvider ContosoRetailDataProvider { get; set; }
        ContosoRetailDataService ContosoRetailDataService { get; set; }

        IEnumerable<Sale> DataSource;

        protected override void OnInitialized() {
            // Refer to https://docs.microsoft.com/dotnet/api/microsoft.aspnetcore.components.owningcomponentbase
            ContosoRetailDataProvider = ScopedServices.GetRequiredService<IContosoRetailDataProvider>();
            ContosoRetailDataService = ScopedServices.GetRequiredService<ContosoRetailDataService>();

            var connectionString = ConnectionStringUtils.GetPivotGridLargeDataConnectionString(Configuration);
            if(string.IsNullOrEmpty(connectionString)) return;

            DataSource = ContosoRetailDataService.GetSales();
        }
    }
</DemoPageSectionComponent>
